package com.zhilianbao.leyaogo.ui.fragment.goodscategory;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.bql.baseadapter.recycleView.QuickRcvAdapter;
import com.bql.convenientlog.CLog;
import com.bql.fragmentation.anim.DefaultNoAnimator;
import com.bql.fragmentation.anim.FragmentAnimator;
import com.bql.pulltorefreshandloadmore.loadmoreview.LoadMoreRecyclerView;
import com.bql.statetypelayout.AnimationStateTypeLayout;
import com.bql.utils.EventManager;
import com.melnykov.fab.FloatingActionButton;
import com.zhilianbao.leyaogo.R;
import com.zhilianbao.leyaogo.events.LeEventCode;
import com.zhilianbao.leyaogo.model.response.category.GoodsDetails;
import com.zhilianbao.leyaogo.ui.adapter.goodscategory.DetailsAdapter;
import com.zhilianbao.leyaogo.ui.fragment.base.RefreshAndLoadFragment;
import com.zhilianbao.leyaogo.utils.DensityUtils;
import com.zhilianbao.leyaogo.utils.LoadStateUtils;
import com.zhilianbao.leyaogo.utils.Utils;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 商品详情页面
 * Created by gyt on 2016/11/2
 */
public class GoodsDetailsFragment extends RefreshAndLoadFragment<String> {

    @BindView(R.id.fab_go_top)
    FloatingActionButton     mFabGoTop;
    @BindView(R.id.stateLayout)
    AnimationStateTypeLayout mStateLayout;
    @BindView(R.id.rcv_load_more)
    LoadMoreRecyclerView     mRcvLoadMore;
    private List<String>          mGoodsPics;
    private OnBackPressedListener mOnBackPressedListener;
    private DetailsAdapter        mDetailsAdapter;
    private GoodsDetails          mGoodsDetails;
    private LoadStateUtils mLoadStateUtils;
    private int totalDy = 0;

    public static GoodsDetailsFragment getInstance() {
        GoodsDetailsFragment fragment = new GoodsDetailsFragment();
        //        Bundle bundle = new Bundle();
        //        bundle.putStringArrayList(BUNDLE_GOODS_PIC, (ArrayList<String>) goodsPics);
        //        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void initLazyViews(@NonNull Bundle savedInstanceState) {
        super.initLazyViews(savedInstanceState);
        mGoodsPics = Utils.getGoodsFiles(mGoodsDetails, 1);

        mLoadStateUtils = new LoadStateUtils(mActivity, getView());
        mDetailsAdapter = new DetailsAdapter(mActivity, mList);
        mRcvLoadMore.setOnScrollListener(new RecyclerView.OnScrollListener() {

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                totalDy += dy;
                if (totalDy >= DensityUtils.dp2px(mActivity, (float) 200)) {
                    mFabGoTop.setVisibility(View.VISIBLE);
                    mFabGoTop.show(true);
                } else {
                    mFabGoTop.hide(true);
                }
            }
        });
    }


    /**
     * 在viewPager的fragment中，要调用该方法否则initViewsAndEvents不会调用
     *
     * @return
     */
    @Override
    protected FragmentAnimator onCreateFragmentAnimator() {
        return new DefaultNoAnimator();
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.fragment_goods_details;
    }

    @Override
    public boolean registerEventBus() {
        return true;
    }

    @Override
    protected void onHandleEvent(EventManager eventManager) {
        super.onHandleEvent(eventManager);
        if (eventManager.getEventCode() == LeEventCode.MSG_CODE_GET_GOODS_DETAILS_SUCS) {
            mGoodsDetails = (GoodsDetails) eventManager.getData();
        }
    }

    @OnClick(R.id.fab_go_top)
    public void onClick(View v) {
        totalDy = 0;
        mRcvLoadMore.scrollToPosition(0);
        mFabGoTop.hide(true);
    }

    /**
     * 点击返回键，scrollView滚动到顶端，并且viewPager切换到第一个fragment
     *
     * @return
     */
    @Override
    public boolean onBackPressedSupport() {
        mOnBackPressedListener.onBackPressed();
        return true;
    }

    public void setOnBackPressedListener(OnBackPressedListener listener) {
        this.mOnBackPressedListener = listener;
    }

    @Override
    public void onLoadMore() {

    }


    public interface OnBackPressedListener {
        void onBackPressed();
    }

    @Override
    protected void initViewsAndEvents(Bundle savedInstanceState) {
    }

    @Override
    protected void initToolbarHere() {

    }


    @Override
    public void onRcvItemClick(RecyclerView.ViewHolder holder, int position) {

    }

    @Override
    public QuickRcvAdapter<String> getAdapter() {
        return mDetailsAdapter;
    }

    @Override
    public RecyclerView.ItemDecoration getItemDecoration() {
        return null;
    }

    @Override
    public RecyclerView.LayoutManager getLayoutManager() {
        return new LinearLayoutManager(mActivity);
    }

    @Override
    public void loadDataList(int curPage, boolean isPullToRefresh) {
        if (mGoodsPics == null || mGoodsPics.size() == 0) {
            mLoadStateUtils.showEmpty(getResources().getString(R.string.hint_no_goods_details_pic));
        } else {
            CLog.i("picsize =============== " + mGoodsPics.size());
            mDetailsAdapter.clear();
            mDetailsAdapter.addToLast(mGoodsPics);
        }
    }

    @Override
    public void onRefresh() {

    }

    @Override
    public boolean canRefresh() {
        return false;
    }

    @Override
    protected boolean lazyLoadMode() {
        return true;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

}
